package com.zzyl.nursing.controller;

import com.huaweicloud.sdk.iotda.v5.model.ServiceCapability;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.nursing.vo.device.DeviceDetailVo;
import com.zzyl.nursing.vo.device.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 设备模块Controller
 * 
 * @author fan
 * @date 2025-06-08
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "设备模块相关接口")
public class DeviceController extends BaseController
{
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询设备模块列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取设备模块列表")
    public TableDataInfo<List<Device>> list(Device device)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 从物联网平台同步产品列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @PostMapping("/syncProductList")
    @ApiOperation(value = "从物联网平台同步产品列表")
    public AjaxResult syncProductList()
    {
        deviceService.syncProductList();
        return success();
    }

    /**
     * 查询所有产品列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/allProduct")
    @ApiOperation(value = "查询所有产品列表")
    public R<List<ProductVo>> allProduct()
    {
        List<ProductVo> list = deviceService.allProduct();
        return R.ok(list);
    }

    /**
     * 注册设备
     */
    @PostMapping("/register")
    @ApiOperation(value = "注册设备")
    public AjaxResult  register(@RequestBody DeviceDto deviceDto)
    {
        deviceService.register(deviceDto);
        return success();
    }

    /**
     * 获取设备详细信息
     */
    @GetMapping("/{iotId}")
    @ApiOperation("获取设备详细信息")
    public R<DeviceDetailVo> getInfo(@PathVariable("iotId") String iotId) {
        return R.ok(deviceService.queryDeviceDetail(iotId));
    }

    /**
     * 修改设备
     */
    @PutMapping
    @ApiOperation(value = "修改设备")
    public AjaxResult edit(@RequestBody DeviceDto deviceDto)
    {

        return toAjax(deviceService.edit(deviceDto));
    }

    /**
     * 删除设备
     */
    @DeleteMapping("/{iotId}")
    @ApiOperation(value = "删除设备")
    public AjaxResult remove(@PathVariable String iotId){

        return toAjax(deviceService.deleteDevice(iotId));
    }

    /**
     * 查询设备上报数据
     */
    @GetMapping("/queryServiceProperties/{iotId}")
    @ApiOperation("查询设备上报数据")
    public AjaxResult queryServiceProperties(@PathVariable("iotId") String iotId) {
        List<Map<String,Object>> list = deviceService.queryServiceProperties(iotId);
        return success(list);
    }

    /**
     * 查询产品详情(报警规则模块使用)
     */
    @GetMapping("/queryProduct/{productKey}")
    @ApiOperation("查询产品详情(报警规则模块使用)")
    public R<List<ServiceCapability>> queryProduct(@PathVariable("productKey") String productKey) {
        return R.ok(deviceService.queryProduct(productKey));
    }
}
